package com.example.myapplication;

import android.app.Service;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import java.util.List;

/**
 * 定位服务
 * @author wxy
 *
 */
public class LocationSvc extends Service implements LocationListener {

    private LocationManager locationManager;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.d("getLocation","----获取位置>>>");
        if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
            locationManager
                    .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                            this);
        }
//        else if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
//            locationManager
//                    .requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
//                            this);
//        }
        else Toast.makeText(this, "无法定位", Toast.LENGTH_SHORT).show();

//        getLocation();
    }

    @Override
    public boolean stopService(Intent name) {
        return super.stopService(name);
    }

    @Override
    public void onLocationChanged(Location location) {

        //通知Activity
        Intent intent = new Intent();
        intent.setAction(Common.LOCATION_ACTION);
        intent.putExtra(Common.LOCATION, location.toString());
        sendBroadcast(intent);

        // 如果只是需要定位一次，这里就移除监听，停掉服务。如果要进行实时定位，可以在退出应用或者其他时刻停掉定位服务。
        locationManager.removeUpdates(this);
        stopSelf();
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    public void getLocation(){
        LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);//高精度
        criteria.setAltitudeRequired(false);//无海拔要求   criteria.setBearingRequired(false);//无方位要求
        criteria.setCostAllowed(false);//允许产生资费   criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗

        // 获取最佳服务对象
        List<String> provider = mLocationManager.getProviders(criteria,true);
        if (null != provider) {
//            Location location = mLocationManager.getLastKnownLocation(provider);
//            Log.d("getLocation","----getLatitude>>>" + location.getLatitude());
//            Log.d("getLocation","----getLongitude>>>" + location.getLongitude());
        }
    }

}